<!doctype html>
<html lang="zh-CN"><head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
    <title>薛尧的博客</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="" />

    
    
    
    <link rel="stylesheet" href="../../../css/theme.min.css">

    
    
    
    
    <link rel="stylesheet" href="../../../css/custom.min.css">
    

    
</head>
<body>
        <div id="content" class="mx-auto"><header class="container mt-sm-5 mt-4 mb-4 mt-xs-1">
    <div class="row">
        
        <div class="col-sm-4 col-12 text-sm-right text-center pt-sm-4">
            <a href="../../../" class="text-decoration-none">
                <img id="home-image" class="rounded-circle"
                    
                        src="https://ueyao.github.io/image-hosting/blog/avatar.png"
                    
                />
            </a>
        </div>
        <div class="col-sm-8 col-12 text-sm-left text-center">
        
            <h2 class="m-0 mb-2 mt-4">
                <a href="../../../" class="text-decoration-none">
                    
                        薛尧
                    
                </a>
            </h2>
            <p class="text-muted mb-1">
                
                    Java Developer | Short Video Creator
                
            </p>
            <ul id="nav-links" class="list-inline mb-2">
                
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../about/" title="关于">关于</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../post/" title="文章">文章</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../categories/" title="分类">分类</a>
                    </li>
                
            </ul>
            <ul id="nav-social" class="list-inline">
                
                    <li class="list-inline-item mr-3">
                        <a href="http://github.com/flowstone" target="_blank">
                            <i class="fab fa-github fa-1x text-muted"></i>
                        </a>
                    </li>
                
                    <li class="list-inline-item mr-3">
                        <a href="mailto:xueyao.me#gmail.com" target="_blank">
                            <i class="fas fa-at fa-1x text-muted"></i>
                        </a>
                    </li>
                
            </ul>
        </div>
    </div>
    <hr />
</header>
<div class="container">
    <div class="pl-sm-2">
        <div class="mb-3">
            <h3 class="mb-0">Spring的AOP编程</h3>
            
            <small class="text-muted">发布于 2017-10-16</small>
        </div>

        <article>
            <p>AOP为Aspect Oriented Programming(面向切面编程)</p>
<p>AOP的好处：在不修改源代码的情况下，可以实现功能的增强</p>
<h2 id="jdk动态代理">JDK动态代理</h2>
<p>缺点：只能针对实现了接口的类实现代理</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"> * Jdk的动态代理
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"> * @author kevin
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"> */</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">JdkProxy</span> <span style="color:#66d9ef">implements</span> InvocationHandler{
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//要代理的对象</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> CustomerDao customerDao;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#a6e22e">JdkProxy</span>(CustomerDao customerDao){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">customerDao</span> <span style="color:#f92672">=</span> customerDao;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    * 生成代理对象的方法
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    * @return
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> CustomerDao <span style="color:#a6e22e">createProxy</span>(){
</span></span><span style="display:flex;"><span>        CustomerDao proxy <span style="color:#f92672">=</span> (CustomerDao) Proxy.<span style="color:#a6e22e">newProxyInstance</span>(customerDao.<span style="color:#a6e22e">getClass</span>().<span style="color:#a6e22e">getClassLoader</span>(), customerDao.<span style="color:#a6e22e">getClass</span>().<span style="color:#a6e22e">getInterfaces</span>(), <span style="color:#66d9ef">this</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> proxy;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    * 增强的方法
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> Object <span style="color:#a6e22e">invoke</span>(Object proxy, Method method, Object<span style="color:#f92672">[]</span> args) <span style="color:#66d9ef">throws</span> Throwable {
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;权限校验...&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> method.<span style="color:#a6e22e">invoke</span>(customerDao, args);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//编写测试代码：</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">test</span>(){
</span></span><span style="display:flex;"><span>    CustomerDao customerDao <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> CustomerDaoImpl();
</span></span><span style="display:flex;"><span>    JdkProxy jdkProxy <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> JdkProxy(customerDao);
</span></span><span style="display:flex;"><span>    CustomerDao proxy <span style="color:#f92672">=</span> jdkProxy.<span style="color:#a6e22e">createProxy</span>();
</span></span><span style="display:flex;"><span>    proxy.<span style="color:#a6e22e">save</span>();
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="cglib动态代理了解">CGLIB动态代理(了解)</h2>
<p>可以针对那些没有实现接口的类实现代理</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CglibProxy</span> <span style="color:#66d9ef">implements</span> MethodInterceptor{
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//要代理的对象</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> LinkManDao linkManDao;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#a6e22e">CglibProxy</span>(LinkManDao linkManDao) {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">linkManDao</span> <span style="color:#f92672">=</span> linkManDao;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> LinkManDao <span style="color:#a6e22e">createProxy</span>(){
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//创建Cglib核心类</span>
</span></span><span style="display:flex;"><span>        Enhancer enhancer <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Enhancer();
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//设置父类</span>
</span></span><span style="display:flex;"><span>        enhancer.<span style="color:#a6e22e">setSuperclass</span>(linkManDao.<span style="color:#a6e22e">getClass</span>());
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//设置回调</span>
</span></span><span style="display:flex;"><span>        enhancer.<span style="color:#a6e22e">setCallback</span>(<span style="color:#66d9ef">this</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//生成代理</span>
</span></span><span style="display:flex;"><span>        LinkManDao proxy <span style="color:#f92672">=</span> (LinkManDao) enhancer.<span style="color:#a6e22e">create</span>();
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> proxy;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> Object <span style="color:#a6e22e">intercept</span>(Object proxy, Method method, Object<span style="color:#f92672">[]</span> args, MethodProxy methodProxy) <span style="color:#66d9ef">throws</span> Throwable {
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;日志记录&#34;</span>);
</span></span><span style="display:flex;"><span>        Object obj <span style="color:#f92672">=</span> methodProxy.<span style="color:#a6e22e">invokeSuper</span>(proxy, args);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> obj;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//编写测试代码</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">test2</span>(){
</span></span><span style="display:flex;"><span>    LinkManDao linkManDao <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> LinkManDao();
</span></span><span style="display:flex;"><span>    CglibProxy cglibProxy <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> CglibProxy(linkManDao);
</span></span><span style="display:flex;"><span>    LinkManDao proxy <span style="color:#f92672">=</span> cglibProxy.<span style="color:#a6e22e">createProxy</span>();
</span></span><span style="display:flex;"><span>    proxy.<span style="color:#a6e22e">save</span>();
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>术语</p>
<ol>
<li>Target: 目标对象。要对类增强，增强的类就称为目标对象。</li>
<li>JoinPoint: 连接点。可以被增强的点就称为JoinPoint。</li>
<li>PointCut: 切入点。在实际开发中真正被增强了的点就称为PointCut。</li>
<li>Advice: 增强(通知)。现在需要对save方法增强一个日志记录的功能，就需要编写一个日志记录的方法。那么这个日志记录的方法就称为Advice。</li>
<li>Weaving: 织入。 把Advice应用到目标的过程叫织入。</li>
<li>Introduction: 引介。 指的是类层面的增强。</li>
<li>Proxy: 代理。 增强之后的对象。</li>
<li>Aspect: 切面。 多个增强和多个切入点的组合就叫切面。</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">UserDaoImpl</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">save</span>(){}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">list</span>(){}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">delete</span>(){}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">update</span>(){}
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="springxml形式的aop入门">SpringXML形式的AOP入门</h2>
<h3 id="配置">配置</h3>
<p>导入jar包（11）
<img src="https://ueyao.github.io/image-hosting/blog/2017/10/2017-10-15_170353.png" alt=""></p>
<p>红线画起来的表示：</p>
<p>AOP联盟的jar包：com.springsource.org.aopalliance-1.0.0.jar</p>
<p>Spring提供的AOP的jar包：spring-aop-4.2.4.RELEASE.jar</p>
<p>AspectJ的jar包：com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar</p>
<p>Spring整合AspectJ的jar包：spring-aspects-4.2.4.RELEASE.jar</p>
<p>配置文件</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;beans</span> <span style="color:#a6e22e">xmlns=</span><span style="color:#e6db74">&#34;http://www.springframework.org/schema/beans&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">xmlns:xsi=</span><span style="color:#e6db74">&#34;http://www.w3.org/2001/XMLSchema-instance&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">xmlns:context=</span><span style="color:#e6db74">&#34;http://www.springframework.org/schema/context&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">xmlns:aop=</span><span style="color:#e6db74">&#34;http://www.springframework.org/schema/aop&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">xsi:schemaLocation=</span><span style="color:#e6db74">&#34;http://www.springframework.org/schema/beans
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/beans/spring-beans.xsd
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/context
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/context/spring-context.xsd
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/aop 
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/aop/spring-aop.xsd&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;bean</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">&#34;customerDao&#34;</span> <span style="color:#a6e22e">class=</span><span style="color:#e6db74">&#34;me.xueyao.dao.impl.CustomerDaoImpl&#34;</span><span style="color:#f92672">&gt;&lt;/bean&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;bean</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">&#34;myAspectXml&#34;</span> <span style="color:#a6e22e">class=</span><span style="color:#e6db74">&#34;me.xueyao.aspect.MyAspectXml&#34;</span><span style="color:#f92672">&gt;&lt;/bean&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- AOP配置 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:config&gt;</span>
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">&lt;!-- 配置切入点 --&gt;</span>
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&lt;aop:pointcut</span> <span style="color:#a6e22e">expression=</span><span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.save(..))&#34;</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">&#34;pointcut1&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">&lt;!-- 配置切面 --&gt;</span>
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&lt;aop:aspect</span> <span style="color:#a6e22e">ref=</span><span style="color:#e6db74">&#34;myAspectXml&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>                <span style="color:#f92672">&lt;aop:before</span> <span style="color:#a6e22e">method=</span><span style="color:#e6db74">&#34;checkPrivilege&#34;</span> <span style="color:#a6e22e">pointcut-ref=</span><span style="color:#e6db74">&#34;pointcut1&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&lt;/aop:aspect&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/aop:config&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/beans&gt;</span>
</span></span></code></pre></div><p>切入点表达式</p>
<p>语法：[修饰符] 返回类型 包名.类名.方法名(形式参数)</p>
<p>常见写法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>execution(<span style="color:#66d9ef">public</span> <span style="color:#f92672">*</span> <span style="color:#f92672">*</span>(..))  所有的public方法
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">execution</span>(<span style="color:#f92672">*</span> set(..))    所有set开头的方法
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">execution</span>(<span style="color:#f92672">*</span> com.<span style="color:#a6e22e">xyz</span>.<span style="color:#a6e22e">service</span>.<span style="color:#a6e22e">AccountService</span>.<span style="color:#f92672">*</span>(..))   AccountService类中的所有方法
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">execution</span>(<span style="color:#f92672">*</span> com.<span style="color:#a6e22e">xyz</span>.<span style="color:#a6e22e">service</span>.<span style="color:#f92672">*</span>.<span style="color:#f92672">*</span>(..))    com.<span style="color:#a6e22e">xyz</span>.<span style="color:#a6e22e">service包下所有的方法</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">execution</span>(<span style="color:#f92672">*</span> com.<span style="color:#a6e22e">xyz</span>.<span style="color:#a6e22e">service</span>..<span style="color:#f92672">*</span>.<span style="color:#f92672">*</span>(..))   com.<span style="color:#a6e22e">xyz</span>.<span style="color:#a6e22e">service包及其子包下所有的方法</span>
</span></span></code></pre></div><p>Spring中AOP的通知类型</p>
<p>1.前置通知： 在方法执行之前增强。可以获得切入点信息。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- 配置切面 --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;aop:aspect</span> <span style="color:#a6e22e">ref=</span><span style="color:#e6db74">&#34;myAspectXml&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:before</span> <span style="color:#a6e22e">method=</span><span style="color:#e6db74">&#34;checkPrivilege&#34;</span> <span style="color:#a6e22e">pointcut-ref=</span><span style="color:#e6db74">&#34;pointcut1&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/aop:aspect&gt;</span>
</span></span></code></pre></div><p>2.后置通知： 在方法执行完之后增强。可以获取返回值信息。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;aop:aspect</span> <span style="color:#a6e22e">ref=</span><span style="color:#e6db74">&#34;myAspectXml&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 前置通知 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:before</span> <span style="color:#a6e22e">method=</span><span style="color:#e6db74">&#34;checkPrivilege&#34;</span> <span style="color:#a6e22e">pointcut-ref=</span><span style="color:#e6db74">&#34;pointcut1&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 后置通知 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:after-returning</span> <span style="color:#a6e22e">method=</span><span style="color:#e6db74">&#34;afterReturn&#34;</span> <span style="color:#a6e22e">pointcut-ref=</span><span style="color:#e6db74">&#34;pointcut2&#34;</span> <span style="color:#a6e22e">returning=</span><span style="color:#e6db74">&#34;result&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/aop:aspect&gt;</span>
</span></span></code></pre></div><p>3.环线通知: 在方法执行前后都进行增强。可以阻止方法的执行。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CustomerDaoImpl</span> <span style="color:#66d9ef">implements</span> CustomerDao {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">update</span>() {
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;持久层：客户更新&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyAspectXml</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> Object <span style="color:#a6e22e">around</span>(ProceedingJoinPoint joinpoint){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;环绕前执行&#34;</span>);
</span></span><span style="display:flex;"><span>        Object obj <span style="color:#f92672">=</span> <span style="color:#66d9ef">null</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">try</span> {
</span></span><span style="display:flex;"><span>            obj <span style="color:#f92672">=</span> joinpoint.<span style="color:#a6e22e">proceed</span>();
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">catch</span> (Throwable e) {
</span></span><span style="display:flex;"><span>            e.<span style="color:#a6e22e">printStackTrace</span>();
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;环绕后执行&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> obj;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- AOP配置 --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;aop:config&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 配置切入点 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:pointcut</span> <span style="color:#a6e22e">expression=</span><span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.update(..))&#34;</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">&#34;pointcut3&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 配置切面 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:aspect</span> <span style="color:#a6e22e">ref=</span><span style="color:#e6db74">&#34;myAspectXml&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">&lt;!-- 环绕通知 --&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;aop:around</span> <span style="color:#a6e22e">method=</span><span style="color:#e6db74">&#34;around&#34;</span> <span style="color:#a6e22e">pointcut-ref=</span><span style="color:#e6db74">&#34;pointcut3&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/aop:aspect&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/aop:config&gt;</span>
</span></span></code></pre></div><p>4.异常抛出通知： 当发生异常之后增强，可以获取异常信息</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CustomerDaoImpl</span> <span style="color:#66d9ef">implements</span> CustomerDao {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">find</span>() {
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;持久层：查询&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> 10<span style="color:#f92672">/</span>0;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyAspectXml</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">afterThrowing</span>(Exception ex){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;抛出异常通知:&#34;</span> <span style="color:#f92672">+</span> ex.<span style="color:#a6e22e">getMessage</span>());
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- AOP配置 --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;aop:config&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 配置切入点 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:pointcut</span> <span style="color:#a6e22e">expression=</span><span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.find(..))&#34;</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">&#34;pointcut4&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 配置切面 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:aspect</span> <span style="color:#a6e22e">ref=</span><span style="color:#e6db74">&#34;myAspectXml&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">&lt;!-- 抛出异常通知 --&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;aop:after-throwing</span> <span style="color:#a6e22e">method=</span><span style="color:#e6db74">&#34;afterThrowing&#34;</span> <span style="color:#a6e22e">pointcut-ref=</span><span style="color:#e6db74">&#34;pointcut4&#34;</span> <span style="color:#a6e22e">throwing=</span><span style="color:#e6db74">&#34;ex&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/aop:aspect&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/aop:config&gt;</span>
</span></span></code></pre></div><p>5.最终通知： 不管是否有异常，都会执行的</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CustomerDaoImpl</span> <span style="color:#66d9ef">implements</span> CustomerDao {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">find</span>() {
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;持久层：查询&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> 10<span style="color:#f92672">/</span>0;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyAspectXml</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">after</span>(){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;最终通知&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- AOP配置 --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;aop:config&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 配置切入点 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:pointcut</span> <span style="color:#a6e22e">expression=</span><span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.find(..))&#34;</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">&#34;pointcut4&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 配置切面 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;aop:aspect</span> <span style="color:#a6e22e">ref=</span><span style="color:#e6db74">&#34;myAspectXml&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">&lt;!-- 最终通知 --&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;aop:after</span> <span style="color:#a6e22e">method=</span><span style="color:#e6db74">&#34;after&#34;</span> <span style="color:#a6e22e">pointcut-ref=</span><span style="color:#e6db74">&#34;pointcut4&#34;</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/aop:aspect&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/aop:config&gt;</span>
</span></span></code></pre></div><p>最终通知和后置通知的区别：</p>
<p>最终通知，不管异常与否，都执行；而后置通知在异常时不执行。</p>
<p>Spring注解形式的AOP入门
创建工程，引入jar包，创建核心配置文件(和XML引入的包相同)</p>
<p>配置文件</p>
<p>applicationContext.xml</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;beans</span> <span style="color:#a6e22e">xmlns=</span><span style="color:#e6db74">&#34;http://www.springframework.org/schema/beans&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">xmlns:xsi=</span><span style="color:#e6db74">&#34;http://www.w3.org/2001/XMLSchema-instance&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">xmlns:aop=</span><span style="color:#e6db74">&#34;http://www.springframework.org/schema/aop&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">xmlns:context=</span><span style="color:#e6db74">&#34;http://www.springframework.org/schema/context&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">xsi:schemaLocation=</span><span style="color:#e6db74">&#34;http://www.springframework.org/schema/beans
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/beans/spring-beans.xsd
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/aop
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/aop/spring-aop.xsd
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/context
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        http://www.springframework.org/schema/context/spring-context.xsd&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>       <span style="color:#f92672">&lt;context:component-scan</span> <span style="color:#a6e22e">base-package=</span><span style="color:#e6db74">&#34;me.xueyao&#34;</span><span style="color:#f92672">&gt;&lt;/context:component-scan&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">&lt;!-- 开启自动代理注解 --&gt;</span>
</span></span><span style="display:flex;"><span>       <span style="color:#f92672">&lt;aop:aspectj-autoproxy&gt;&lt;/aop:aspectj-autoproxy&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/beans&gt;</span>
</span></span></code></pre></div><p>创建接口和实现类</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">CustomerDao</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * 持久层：客户保存
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">save</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">int</span> <span style="color:#a6e22e">delete</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">update</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">find</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Repository</span>(<span style="color:#e6db74">&#34;customerDao&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CustomerDaoImpl</span> <span style="color:#66d9ef">implements</span> CustomerDao {
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">save</span>() {
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;持久层：客户保存...&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">int</span> <span style="color:#a6e22e">delete</span>(){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;持久层：客户删除...&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> 100;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">update</span>() {
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;持久层：客户更新&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">find</span>() {
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;持久层：查询&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>编写切面类</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Component</span>(<span style="color:#e6db74">&#34;myAspectAnnotation&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Aspect</span> <span style="color:#75715e">//可以省略id</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyAspect</span> {
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Before</span>(<span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.save(..))&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">checkPrivilege</span>(JoinPoint joinPoint){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;权限校验...&#34;</span> <span style="color:#f92672">+</span> joinPoint.<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>Spring的AOP中注解通知</p>
<p>1.前置通知</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">* 前置通知
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">* @param joinPoint
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Before</span>(<span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.save(..))&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">checkPrivilege</span>(JoinPoint joinPoint){
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;权限校验...&#34;</span> <span style="color:#f92672">+</span> joinPoint.<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>2.后置通知</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Aspect</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyAspect</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    * 后置通知
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    * @param result
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@AfterReturning</span>(value<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.delete(..))&#34;</span>,returning<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;result&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">afterReturning</span>(Object result){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;后置通知:&#34;</span> <span style="color:#f92672">+</span> result);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>3.环绕通知</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Around</span>(<span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.update(..))&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> Object <span style="color:#a6e22e">after</span>(ProceedingJoinPoint joinpoint) <span style="color:#66d9ef">throws</span> Throwable{
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;环绕通知前增强&#34;</span>);
</span></span><span style="display:flex;"><span>    Object obj <span style="color:#f92672">=</span> joinpoint.<span style="color:#a6e22e">proceed</span>();
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;环绕通知后增强&#34;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> obj;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>4.异常通知</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@AfterThrowing</span>(value<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.find(..))&#34;</span>,throwing<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;ex&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">afterThrowing</span>(Exception ex){
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;抛出异常通知&#34;</span>);
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>5.最终通知</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@After</span>(<span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.find(..))&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">after</span>(){
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;最终通知&#34;</span>);
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>6.PointCut注解(了解)</p>
<p>作用：用于定义切入点表达式的一个注解。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Component</span>(<span style="color:#e6db74">&#34;myAspectAnnotation&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Aspect</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyAspect</span> {
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        * 前置通知
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        * @param joinPoint
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Before</span>(<span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.save(..))&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">checkPrivilege</span>(JoinPoint joinPoint){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;权限校验...&#34;</span> <span style="color:#f92672">+</span> joinPoint.<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@AfterReturning</span>(value<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.delete(..))&#34;</span>,returning<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;result&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">afterReturning</span>(Object result){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;后置通知:&#34;</span> <span style="color:#f92672">+</span> result);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Around</span>(<span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.update(..))&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> Object <span style="color:#a6e22e">aroung</span>(ProceedingJoinPoint joinpoint) <span style="color:#66d9ef">throws</span> Throwable{
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;环绕通知前增强&#34;</span>);
</span></span><span style="display:flex;"><span>        Object obj <span style="color:#f92672">=</span> joinpoint.<span style="color:#a6e22e">proceed</span>();
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;环绕通知后增强&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> obj;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//  @AfterThrowing(value=&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.find(..))&#34;,throwing=&#34;ex&#34;)</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@AfterThrowing</span>(value<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;MyAspect.pointcut()&#34;</span>,throwing<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;ex&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">afterThrowing</span>(Exception ex){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;抛出异常通知&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//  @After(&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.find(..))&#34;)</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@After</span>(<span style="color:#e6db74">&#34;MyAspect.pointcut()&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">after</span>(){
</span></span><span style="display:flex;"><span>        System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;最终通知&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Pointcut</span>(<span style="color:#e6db74">&#34;execution(* me.xueyao.dao.impl.CustomerDaoImpl.find(..))&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">pointcut</span>(){
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div>
        </article>
    </div>

    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xie-yao-de-bo-ke" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

            </div>
        </div><footer class="text-center pb-1">
    <small class="text-muted">
        
        &copy; Copyright 2024
        
        |
        <a href="https://beian.miit.gov.cn/" target="_blank">苏ICP备14012079号</a>
        <br />
        由 <a href="https://gohugo.io/" target="_blank">Hugo</a> 强力驱动
        | 主题 <a href="https://github.com/austingebauer/devise" target="_blank">Devise</a>
        <br />

    </small>
</footer></body>
</html>
